package com.base.qrcode;

import android.os.Vibrator;
import android.view.View;


import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.base.commonlib.base.BaseApp;
import com.base.commonlib.base.CBaseBarActivity;
import com.base.commonlib.utils.ToastUtil;
import com.base.qrcode.code.QRCodeView;
import com.base.qrcode.databinding.ActivityQrcodeBinding;
import com.base.qrcode.utils.Matchers;

import java.util.regex.Pattern;


/**
 * 二维码扫描
 *
 * @version V1.0 <描述当前版本功能>
 *          Created by admin on 2018/8/17.
 * @FileName: www.madman.com.ui.qrcode.act.QRCodeActivity.java
 * Author weikailiang
 * @date: 2018-08-17 13:11
 */
@Route(path = "/qrcode/code")
public class QRCodeActivity extends CBaseBarActivity implements QRCodeView.Delegate{
    public static final int REQUEST_CODE_CHOOSE_QRCODE_FROM_GALLERY = 666;


    private ActivityQrcodeBinding mBinding;
    private boolean light = false;

    @Override
    protected void onStart() {
        super.onStart();

        mBinding.zxingview.startCamera(); // 打开后置摄像头开始预览，但是并未开始识别
//        mZXingView.startCamera(Camera.CameraInfo.CAMERA_FACING_FRONT); // 打开前置摄像头开始预览，但是并未开始识别

        mBinding.zxingview.startSpotAndShowRect(); // 显示扫描框，并且延迟0.5秒后开始识别
    }

    @Override
    protected void onResume() {
        super.onResume();
        mBinding.zxingview.startSpot(); // 延迟0.5秒后开始识别
    }

    @Override
    protected void onStop() {
        mBinding.zxingview.stopCamera(); // 关闭摄像头预览，并且隐藏扫描框
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        mBinding.zxingview.onDestroy(); // 销毁二维码扫描控件
        super.onDestroy();
    }

    @Override
    protected int getLayoutResource() {
        return R.layout.activity_qrcode;
    }

    private void vibrate() {
        Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
        vibrator.vibrate(200);
    }

    @Override
    public void onScanQRCodeSuccess(String result) {
        vibrate();
        if(result == null){
            ToastUtil.show("未识别");
            mBinding.zxingview.startSpot(); // 延迟0.5秒后开始识别
            return;
        }
        Pattern pattern = Pattern.compile(Matchers.WEB_SITE);


        if (pattern.matcher(result).matches()&&!result.endsWith(".apk")&&!result.endsWith(".APK")){
            //是一个网址
            ARouter.getInstance().build("/qrcode/coderesult")
                    .withString("result",result)
                    .navigation();
//            ARouter.getInstance().build("/sweb/common")
//                    .withInt("key",4)
//                    .withString("url",result)
//                    .navigation();
        }else {
            //字符串
            ARouter.getInstance().build("/qrcode/coderesult")
                    .withString("result",result)
                    .navigation();
        }


    }

    @Override
    public void onScanQRCodeOpenCameraError() {
        showError();
    }


    @Override
    protected void init() {
        super.init();
        mBinding = (ActivityQrcodeBinding) mRootBinding;
        mBinding.zxingview.setRectWidth(BaseApp.SCREEN_WIDTH-100);
        mBinding.zxingview.setRectHeight(BaseApp.SCREEN_WIDTH);
        setBackNavigation();
        setBarTitle("二维码");
    }

    @Override
    protected void start() {
        mBinding.zxingview.setDelegate(this);
        mBinding.qrcodeLight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!light){
                    mBinding.zxingview.openFlashlight();
                    mBinding.qrcodeLight.setImageResource(R.mipmap.ic_night_on);
                }else {
                    mBinding.zxingview.closeFlashlight();
                    mBinding.qrcodeLight.setImageResource(R.mipmap.ic_night_off);
                }
                light = !light;
            }
        });
    }
}
